#include <SDL/SDL.h>
#include "cubeMap.h"
#include "sdlglutils.h"

#define BACK   "textures/stainedglass05.jpg"
#define BOTTOM "textures/stainedglass05.jpg"
#define FRONT  "textures/stainedglass05.jpg"
#define LEFT   "textures/stainedglass05.jpg"
#define RIGHT  "textures/stainedglass05.jpg"
#define TOP    "textures/stainedglass05.jpg"


GLuint CubeMap::getID()
{
    return _id;
}

bool CubeMap::isStatic()
{
    return _isStatic;
}


CubeMap::CubeMap(bool isStatic)
{
    _isStatic=isStatic;
// On génère un ID de texture unique pour les 6 textures du CubeMap
    glGenTextures(1, &_id);


}

CubeMap::~CubeMap()
{
     glDeleteTextures(1,&_id);

}

void CubeMap::displayCubeMapTexture(GLenum  target)
{
    GLint textureWidth, textureHeight;
    unsigned char* textureBuf;
    //     GLubyte *buffer = (GLubyte *)malloc(textureWidth*textureHeight*4);


    // first: get cubemap size
    glBindTexture(GL_TEXTURE_CUBE_MAP,_id);
    glGetTexLevelParameteriv(target, 0, GL_TEXTURE_WIDTH,  &textureWidth);
    glGetTexLevelParameteriv(target, 0, GL_TEXTURE_HEIGHT, &textureHeight);
    textureBuf = new unsigned char [3*textureWidth * textureHeight];
    // second: get texture back
    glGetTexImage(target, 0, GL_RGB, GL_UNSIGNED_BYTE, textureBuf);
    glBindTexture(GL_TEXTURE_CUBE_MAP,0 );

    /*
        Uint32 rmask, gmask, bmask, amask;
    #if SDL_BYTEORDER == SDL_BIG_ENDIAN

        rmask = 0xff000000;
        gmask = 0x00ff0000;
        bmask = 0x0000ff00;
        amask = 0x000000ff;
    #else

        rmask = 0x000000ff;
        gmask = 0x0000ff00;
        bmask = 0x00ff0000;
        amask = 0xff000000;
    #endif

        SDL_Surface * surface = SDL_CreateRGBSurface(SDL_SWSURFACE, textureWidth, textureHeight, 32,
                                rmask, gmask, bmask, amask);
                                surface->pixels=textureBuf;


        SDL_SaveBMP(surface,"fbo");
        SDL_FreeSurface(surface);
      */
    GLuint texid;
    glGenTextures(1,&texid);

    // bind texture
    glBindTexture(GL_TEXTURE_2D,texid);

    // set texture parameters

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    // allocate memory and load data
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0,
                 GL_RGB, GL_UNSIGNED_BYTE, textureBuf);

    glBegin(GL_QUADS);
    glTexCoord2d(0.0, 0.0);
    glVertex3d(0.0, 2.0, 0.0);  // Bottom Left Of The Texture and Quad
    glTexCoord2d(1.0, 0.0);
    glVertex3d( 0.0, 5.0,0.0);  // Bottom Right Of The Texture and Quad
    glTexCoord2d(1.0, 1.0);
    glVertex3d( 0.0, 5.0,3.0);  // Top Right Of The Texture and Quad
    glTexCoord2d(0.0, 1.0);
    glVertex3d(0.0,  2.0,3.0);  // Top Left Of The Texture and Quad
    glEnd();
    glBindTexture(GL_TEXTURE_2D,0);
    glDeleteTextures(1,&texid);


}

void CubeMap::loadCubeMapTexture(void) // Fonction de chargement des textures en mémoires
{

    GLenum CubeMapTarget[6] =             // Liste des targets pour la création des textures de CubeMap
    {
        GL_TEXTURE_CUBE_MAP_POSITIVE_X,
        GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
        GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
        GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
        GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
        GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
    };

    char CubeMapFileName[6][30]=      // Liste des noms de fichiers des textures
    {
        RIGHT,
        LEFT,
        BOTTOM,
        TOP,
        FRONT,
        BACK
    };

    //glActiveTexture(GL_TEXTURE1);

    glBindTexture(GL_TEXTURE_CUBE_MAP, _id);

    SDL_Surface * image=NULL;

    if (_isStatic)
    {
        for (int i = 0; i < 6; i++)
        {
            SDL_Surface * image=loadTextureImage(CubeMapFileName[i]);
            glTexImage2D(CubeMapTarget[i], 0, GL_RGBA,image->w,
                         image->h, 0, GL_RGBA,GL_UNSIGNED_BYTE,
                         image->pixels);
        }
        SDL_FreeSurface(image);
    }
    else
    {
        int w=256;
        int h=256;
        for (int i = 0; i < 6; i++)
        {

            glTexImage2D(CubeMapTarget[i], 0, GL_RGBA,w,
                         h, 0, GL_RGBA,GL_UNSIGNED_BYTE,
                         0);
        }

    }

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP);

    glBindTexture(GL_TEXTURE_CUBE_MAP,0);
    //glActiveTexture(GL_TEXTURE0);
}
